package com.example.springboottools.utils;

public class AliyunRefreshUtils {
    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.cdn20180510.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "cdn.aliyuncs.com";
        return new com.aliyun.cdn20180510.Client(config);
    }

    /**
     * 使用STS鉴权方式初始化账号Client，推荐此方式。本示例默认使用AK&SK方式。
     * @param accessKeyId
     * @param accessKeySecret
     * @param securityToken
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.cdn20180510.Client createClientWithSTS(String accessKeyId, String accessKeySecret, String securityToken) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret)
                // 必填，您的 Security Token
                .setSecurityToken(securityToken)
                // 必填，表明使用 STS 方式
                .setType("sts");
        // 访问的域名
        config.endpoint = "cdn.aliyuncs.com";
        return new com.aliyun.cdn20180510.Client(config);
    }
     public  static String refreshUrl(String pathUrl,String fileType) throws Exception {
         // 工程代码泄露可能会导致AccessKey泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
         com.aliyun.cdn20180510.Client client = createClient("LTAI5tH3C8bZXz9LcfCr3XmG", "Ya07wpjVOEVfrR4t3w1v7G1O98QpHL");
         com.aliyun.cdn20180510.models.RefreshObjectCachesRequest refreshObjectCachesRequest = new com.aliyun.cdn20180510.models.RefreshObjectCachesRequest();
         com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
       //  refreshObjectCachesRequest.setObjectPath("http://newvp-pic.staplescn.com/vpimageServer/7700586779_1.jpg");
         //refreshObjectCachesRequest.setObjectType("File");
         // refreshObjectCachesRequest.setObjectPath("http://newvp-pic.staplescn.com/vpimageServer/");
         // refreshObjectCachesRequest.setObjectType("Directory");
           refreshObjectCachesRequest.setObjectPath(pathUrl);
         refreshObjectCachesRequest.setObjectType(fileType);
         com.aliyun.cdn20180510.models.RefreshObjectCachesResponse resp = client.refreshObjectCachesWithOptions(refreshObjectCachesRequest, runtime);
        return com.aliyun.teautil.Common.toJSONString(resp);

     }
}
